<?php

class Tourism_enshrine_model extends BZ_Model
{
    protected $table   = 'tourism_enshrine';
    protected $table_2 = 'tourism_goods_attr';
    protected $table_3 = 'tourism_goods_base';

    public function insertTourismEnshrine($goodsAttrId, $uid)
    {
        $data = array(
            'uid'           => $uid,
            'goods_attr_id' => $goodsAttrId,
            'created_at'    => date('Y-m-d H:i:s')
        );
        $this->db->insert($this->table,$data);
        return $this->db->insert_id();
    }

    public function findIsExist($goodsAttrId, $uid)
    {
        $this->db->select('tourism_enshrine.id');
        $this->db->from($this->table.' AS tourism_enshrine');
        $this->db->join($this->table_2.' AS tourism_goods_attr', 'tourism_goods_attr.goods_attr_id=tourism_enshrine.goods_attr_id', 'inner');
        $this->db->where('uid', $uid);
        $this->db->where('tourism_enshrine.goods_attr_id', $goodsAttrId);
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 收藏商品总页数
     * @param $params
     * @return mixed
     */
    public function tourismEnshrineTotal($params)
    {
        $this->db->select('tourism_enshrine.id');
        $this->db->from($this->table.' AS tourism_enshrine');
        $this->db->join($this->table_2.' AS tourism_goods_attr', 'tourism_goods_attr.goods_attr_id = tourism_enshrine.goods_attr_id', 'INNER');
        $this->db->join($this->table_3.' AS tourism_goods_base', 'tourism_goods_base.goods_id = tourism_goods_attr.goods_id', 'INNER');
        $this->db->where('tourism_enshrine.uid', $params['uid']);
        return $this->db->count_all_results();
    }

    /**
     * 商品收藏列表
     * @param $params
     * @param $count
     * @param int $start
     * @return mixed
     */
    public function tourismEnshrineList($params, $count, $start = 0)
    {
        $this->db->select('tourism_enshrine.id AS enshrine_id , tourism_goods_attr.goods_attr_id,`tourism_goods_base`.`goods_name`, `tourism_goods_attr`.`attribute_value`, `tourism_goods_attr`.`shop_price`, `tourism_goods_base`.`images`');
        $this->db->from($this->table.' AS tourism_enshrine');
        $this->db->join($this->table_2.' AS tourism_goods_attr', 'tourism_goods_attr.goods_attr_id = tourism_enshrine.goods_attr_id', 'INNER');
        $this->db->join($this->table_3.' AS tourism_goods_base', 'tourism_goods_base.goods_id = tourism_goods_attr.goods_id', 'INNER');
        $this->db->where('tourism_enshrine.uid', $params['uid']);
        $this->db->order_by('tourism_enshrine.id', 'DESC');
        $this->db->limit($start, $count);
        return $this->db->get();
    }

    /**
     * 批量删除收藏夹
     * @param $ids
     * @return mixed
     */
    public function deleteEnshrine($ids)
    {
        $this->db->where_in('id', $ids);
        return $this->db->delete($this->table);
    }
}